﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using BusinessLogic;
using System.Data;
using DataAccess;

namespace Manager
{
    public partial class AddNews : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            InitEditor();
            if (!Page.IsPostBack)
            {
                Load_Data();
            }
        }

        protected void InitEditor()
        {
            editorContent.UIColor = System.Drawing.Color.DarkGreen;
            editorContent.config.toolbar = new object[]
			{
				new object[] { "Source", "-", "Bold", "Italic", "Underline", "Strike", "-", "Subscript", "Superscript" },
				new object[] { "NumberedList", "BulletedList", "-", "Outdent", "Indent", "Blockquote", "CreateDiv" },
				new object[] { "JustifyLeft", "JustifyCenter", "JustifyRight", "JustifyBlock" },
				new object[] { "BidiLtr", "BidiRtl" },
				new object[] { "Link", "Unlink", "Anchor" },
				new object[] { "Image", "Flash", "Table", "HorizontalRule", "Smiley", "SpecialChar", "PageBreak", "Iframe" },
				"/",
				new object[] { "Styles", "Format", "Font", "FontSize" },
				new object[] { "TextColor", "BGColor" }
			};

            CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
            _FileBrowser.BasePath = "/ckfinder/";
            _FileBrowser.SetupCKEditor(editorContent);
        }

        private void Load_Data()
        {
            imgThumb.ImageUrl = Config.Global.Settings.ImagePath + "NoImage.gif";
            lblDatePosted.Text = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss");
        }

        #region btnAdd Click
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtNewsTitle.Text.Trim() == "")
            {
                lblMessage.Visible = true;
                lblMessage.Text = "Bạn chưa nhập tiêu đề bài viết.";
                txtNewsTitle.Focus();
                return;
            }

            NewsInfo newsInfo = new NewsInfo();

            newsInfo.SiteID = Config.Global.Settings.SiteID;
            newsInfo.NewsTitle = txtNewsTitle.Text.Trim();
            string img = hiddenImg.Value;
            int pos = img.LastIndexOf('/');
            img = img.Substring(pos + 1);
            newsInfo.NewsImage = img;
            newsInfo.DatePosted = DateTime.Now;
            if (rbtActive.Checked)
            {
                newsInfo.NewsStatus = true;
            } 
            else
            {
                newsInfo.NewsStatus = false;
            }
            newsInfo.NewsDescription = txtDes.Text.Trim();
            newsInfo.NewsContent = editorContent.Text;

            try
            {
                NewsData.Insert(newsInfo);
                Response.Redirect("NewsManager.aspx");
            }
            catch (System.Exception ex)
            {
                lblMessage.Visible = true;
                lblMessage.Text = ex.Message;
            }
        }
        #endregion

        protected void lblDel_Click(object sender, EventArgs e)
        {
           imgThumb.ImageUrl = Config.Global.Settings.ImagePath + "NoImage.gif";
           hiddenImg.Value = "";
        }
    }
}